package com.example.admin.controller.InterviewRecord;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.airobotinterviewrecord.entity.InterviewRecover;
import com.example.airobotinterviewrecord.service.IInterviewRecoverService;
import com.example.question.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/interviewRecover")
public class InterviewRecoverController {

    @Autowired
    private IInterviewRecoverService interviewRecoverService;

    // 1. 新增接口
    @PostMapping("addRecover")
    public Result<String> create(@Valid @RequestBody InterviewRecover record) {
        boolean success = interviewRecoverService.save(record);
        return success ? Result.success("创建成功") : Result.error("创建失败");
    }

    // 2. 分页全查接口
    @GetMapping("/page")
    public Result<Page<InterviewRecover>> pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer candidateId,
            @RequestParam(required = false) Integer questionId) {

        Page<InterviewRecover> page = interviewRecoverService.pageList(
                pageNum, pageSize, candidateId, questionId);
        return Result.success(page);
    }

    // 3. 根据ID删除
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        boolean success = interviewRecoverService.removeById(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    // 4. 根据ID修改
    @PutMapping("/updateRecover")
    public Result<String> update(@Valid @RequestBody InterviewRecover record) {
        if (record.getCandidateId() == null) {
            return Result.error("ID不能为空");
        }
        boolean success = interviewRecoverService.updateById(record);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }
}